package com.zhj.rabbitmq.demo.delayqueue;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengjibin
 * @version 1.0.0
 * @ClassName CreateTTLQueueConfig.java
 * @Description 创建交换机和队列
 * @createTime 2023年03月22日 21:05:00
 */
@Configuration
public class CreateTTLQueueConfig {
    //普通交换机
    public static final String P_EXCHANGE = "p_exchange";
    //普通队列1 10秒过期
    public static final String P_QUEUE_1 = "p_queue_1";
    //普通队列2 40秒过期
    public static final String P_QUEUE_2 = "p_queue_2";
    //普通队列2 过期时间由生产端决定
    public static final String P_QUEUE_3 = "p_queue_3";

    //死信交换机
    public static final String S_EXCHANGE = "s_exchange";
    //死信队列
    public static final String S_QUEUE = "s_queue_1";

    /**
     * 创建普通交换机
     * 交换机类型direct
     */
    @Bean("pExchange")
    public DirectExchange pExchange(){
        return new DirectExchange(CreateTTLQueueConfig.P_EXCHANGE);
    }

    /**
     * 创建死信交换机
     * 交换机类型direct
     */
    @Bean("sExchange")
    public DirectExchange sExchange(){
        return new DirectExchange(CreateTTLQueueConfig.S_EXCHANGE);
    }

    /**
     * 创建普通队列1
     */
    @Bean(P_QUEUE_1)
    public Queue p_queue1(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",S_EXCHANGE);//指定死信交换机,x-dead-letter-exchange参数固定
        map.put("x-dead-letter-routing-key",S_ROUTINGKEY);//死信routingkey指定
        map.put("x-message-ttl",20000);//设置ttl 过期时间，单位ms
        return QueueBuilder.durable(P_QUEUE_1).withArguments(map).build();
    }

    /**
     * 创建普通队列2
     */
    @Bean(P_QUEUE_2)
    public Queue p_queue2(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",S_EXCHANGE);//指定死信交换机,x-dead-letter-exchange参数固定
        map.put("x-dead-letter-routing-key",S_ROUTINGKEY);//死信routingkey指定
        map.put("x-message-ttl",30000);//设置ttl 过期时间，单位ms
        return QueueBuilder.durable(P_QUEUE_2).withArguments(map).build();
    }

    /**
     * 创建普通队列3
     */
    @Bean(P_QUEUE_3)
    public Queue p_queue3(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",S_EXCHANGE);//指定死信交换机,x-dead-letter-exchange参数固定
        map.put("x-dead-letter-routing-key",S_ROUTINGKEY);//死信routingkey指定
        //这不在知道队列ttl 过期时间，由生产端决定
        return QueueBuilder.durable(P_QUEUE_3).withArguments(map).build();
    }

    /**
     * 创建死信队列
     */
    @Bean(S_QUEUE)
    public Queue s_queue(){
        return QueueBuilder.durable(S_QUEUE).build();
    }

    public static String P_ROUTINGKEY1 = "routingkey1";
    public static String P_ROUTINGKEY2 = "routingkey2";
    public static String P_ROUTINGKEY3 = "routingkey3";
    public static String S_ROUTINGKEY = "srk";
    /**
     * 绑定普通队列1和普通交换机
     */
    @Bean
    public Binding bindingPQ1PExchange(@Qualifier(P_QUEUE_1) Queue queue ,@Qualifier("pExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(P_ROUTINGKEY1);
    }

    /**
     * 绑定普通队列2和普通交换机
     */
    @Bean
    public Binding bindingPQ2PExchange(@Qualifier(P_QUEUE_2) Queue queue ,@Qualifier("pExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(P_ROUTINGKEY2);
    }
    /**
     * 绑定普通队列3和普通交换机
     */
    @Bean
    public Binding bindingPQ3PExchange(@Qualifier(P_QUEUE_3) Queue queue ,@Qualifier("pExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(P_ROUTINGKEY3);
    }

    /**
     * 绑定死信交换机和死信队列
     */
    @Bean
    public Binding bindingSQPExchange(@Qualifier(S_QUEUE) Queue queue ,@Qualifier("sExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(S_ROUTINGKEY);
    }
}
